<?php
function alert($flag,$data){
    echo json_encode(array('flag'=>$flag,'data'=>$data));
    exit;
}
function email($email){
    $pattern="/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    if(preg_match($pattern,$email)){
        return true;
    }
    return false;
}
function pushjava($url,$post_data){
    $ch = curl_init();
	$headers = array();
	$headers[] = 'Content-Type: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT,60);//单位秒,也可以使用
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
function push($url,$post_data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT,60);//单位秒,也可以使用
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
    $output = curl_exec($ch);
    curl_close($ch);
    return trim($output,'﻿');//替换掉utf8 BOM格式编码字符
}
function send_curl($url,$data){
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	 curl_setopt($ch, CURLOPT_TIMEOUT,60);//单位秒,也可以使用
     //post传值数据
     curl_setopt($ch, CURLOPT_POST, 1);
     //post的变量
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data'=>json_encode($data))));
     $output = curl_exec($ch);

     curl_close($ch);
     return trim($output,'﻿');//替换掉utf8 BOM格式编码字符
 }
//////权限检查  有则显示对应的按钮，没有则不显示
function root($a,$c=CONTROLLER_NAME){
    $root=session('user_root');
    $is_root = 0;
    foreach ($root as $k => $v) {
        if ($k == $c && in_array($a, $v)) {
            $is_root = 1;
        }
    }
    if ($is_root==0) {
        $no_arr = array(//额外的都具备的权限
            'Index' => array('index', 'welcome'),
            'Unit' => array('getUnit'),
            'Category' => array('getCategory'),
            'Role' => array('getRole', 'getEditRole'),
            'List' => array('warehouse_select_session_delete', 'warehouse_select'),
        );
        foreach($no_arr as $k=>$v){
            if($k == $c && in_array($a, $v)){
                $is_root=1;
            }
        }
        if($is_root==1){
            return 1;
        }else{
            return 0;
        }
    }
}
//税费计算：税=消费税+增值税
//完税价格=货款+运费+保费
//消费税=完税价格/(1-消费税税率)*消费税税率*0.7
//增值税=(完税价格+消费税)*增值税税率*0.7
//0.7:折扣率
//$total_price:单个商品货款 $ems_fee:运费$insure_amount:保费$consumption_rate:消费税率,$increment_rate:增值税率,$discount_rate:折扣率
// function total_tax($total_price,$ems_fee,$insure_amount,$consumption_rate,$increment_rate,$discount_rate=0.7){
	// $wan_fee=$total_price+$ems_fee+$insure_amount;//完税价格
	// $consumption_fee=$wan_fee/(1-$consumption_rate)*$consumption_rate*$discount_rate;//消费税
	// $increment_fee=($wan_fee+$consumption_fee)*$increment_rate*$discount_rate;
	// return $consumption_fee+$increment_fee;
// }

//税费计算：税=消费税+增值税
//完税价格=货款+运费+保费
//消费税=完税价格/(1-消费税税率)*消费税税率*0.7
//增值税=(完税价格+消费税)*增值税税率*0.7
//+++++++++++++++++++++++++++++++++++++++++++++
//增值税（公式2,消费税率不为0应用此公式）= 完税价格/(1-消费税税率)*增值税综合税率*0.7
//0.7:折扣率
//$total_price:单个商品货款 $ems_fee:运费$insure_amount:保费 $consumption_rate :消费税率,$increment_rate:增值税率,$discount_rate:折扣率
function total_tax($total_price,$ems_fee,$insure_amount,$consumption_rate,$increment_rate,$discount_rate=0.7){
	$wan_fee=$total_price+$ems_fee+$insure_amount;//完税价格
	$consumption_fee=$wan_fee/(1-$consumption_rate)*$consumption_rate*$discount_rate;//消费税
	if($consumption_rate>0){
		$increment_fee=$wan_fee/(1-$consumption_rate)*$increment_rate*$discount_rate;
	}else{
		$increment_fee=($wan_fee+$consumption_fee)*$increment_rate*$discount_rate;
	}
	
	return $consumption_fee+$increment_fee;
}